package com.xianbeiying.app.ckgl.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.PopWindowWpAdapter;
import com.xianbeiying.app.ckgl.adapter.VarbGridViewAdapter;
import com.xianbeiying.app.ckgl.data.DataUtil;
import com.xianbeiying.app.ckgl.widght.MyGridView;
import com.xianbeiying.app.ckgl.widght.datepop.MemoPopWin;
import com.xianbeiying.app.ckgl.widght.popwindow.LayoutPopWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * 风机
 */
public class VaFragment extends Fragment implements View.OnClickListener{
    private View view;
    //水平滑动的
    private LinearLayout ly_horizontal;
    private View va_line;
    private  TextView tv_va_all;
    private TextView tv_va_choose;
    private ImageView iv_va_choose;
    private  LinearLayout ly_rb_manul;
    private LinearLayout ly_rb_auto;
    //手动
    private RadioGroup rd_va_manul;
    private RadioButton va_open;
    private LinearLayout ly_choice_va_manul;
    private MyGridView va_manulgrid;
    private ImageView manul_choice_fj;
    private TextView va_manul_fj;
    private TextView tv_va_manul_end;
    private TextView tv_va_manul_start;
//    private VarbGridViewAdapter vamanuAdpter;
    //自动
    private RadioGroup rd_va_auto;
    private RadioButton va_start;
    private LinearLayout ly_choice_va_auto;
    private MyGridView va_autogrid;
    private ImageView auto_choice_fj;
    private TextView va_auto__fj;
    private static VaFragment vaFragment;
    public static VaFragment getInstance(){
        vaFragment=new VaFragment();
        return vaFragment;
    }
    //泡泡窗口
    private LayoutPopWindow manulPop,autoPop;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       view=inflater.inflate(R.layout.fragment_va,null);
        initView();
        return view;
    }
    private void initView(){
        ly_horizontal=(LinearLayout) view.findViewById(R.id.ly_horizontal);
        va_line=(View) view.findViewById(R.id.va_line);
        va_manul_fj=(TextView) view.findViewById(R.id.va_manul_fj);
        manul_choice_fj=(ImageView) view.findViewById(R.id.manul_choice_fj);
        tv_va_all=(TextView) view.findViewById(R.id.tv_va_all);
        ly_rb_manul=(LinearLayout) view.findViewById(R.id.ly_rb_manul);
        ly_rb_auto=(LinearLayout) view.findViewById(R.id.ly_rb_auto);
        tv_va_choose=(TextView) view.findViewById(R.id.tv_va_choose);
        iv_va_choose=(ImageView) view.findViewById(R.id.iv_va_choose);
        rd_va_manul=(RadioGroup) view.findViewById(R.id.rd_va_manul);
        rd_va_auto=(RadioGroup) view.findViewById(R.id.rd_va_auto);
        va_open=(RadioButton) view.findViewById(R.id.va_open);
        va_start=(RadioButton)view.findViewById(R.id.va_start);
        ly_choice_va_manul=(LinearLayout) view.findViewById(R.id.ly_choice_va_manul);
        ly_choice_va_auto=(LinearLayout) view.findViewById(R.id.ly_choice_va_auto);
        va_manulgrid=(MyGridView) view.findViewById(R.id.va_manulgrid);
        va_autogrid=(MyGridView) view.findViewById(R.id.va_autogrid);
        auto_choice_fj=(ImageView) view.findViewById(R.id.auto_choice_fj);
        va_auto__fj=(TextView) view.findViewById(R.id.va_auto__fj);
        tv_va_manul_end=(TextView) view.findViewById(R.id.tv_va_manul_end);
        tv_va_manul_start=(TextView) view.findViewById(R.id.tv_va_manul_start);
        initData();
        initEvent();
        va_open.setChecked(true);
    }
    private void initData() {
        getData(DataUtil.dapeng.length,0);
    }
    private List<View> t_list;
    private void getData(int size, final int id){
        t_list = new ArrayList<View>();
        for (int i = 0; i <size; i++) {
            //每个textview的宽度
            View view =LayoutInflater.from(getActivity()).inflate(R.layout.fragment_va_item,null);
            final LinearLayout ly_va_cicy = (LinearLayout) view.findViewById(R.id.ly_va_cicy);
            final TextView tv_va_cicy = (TextView) view.findViewById(R.id.tv_va_cicy);
            tv_va_cicy.setText((i+1)+"\n号棚");
            if(i==id){
                ly_va_cicy.setSelected(true);
                tv_va_cicy.setSelected(true);
            }
            //添加集合
            t_list.add(view);
            //绑定
            ly_va_cicy.setTag(i);
            ly_horizontal.addView(view);
            ly_va_cicy.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    // TODO Auto-generated method stub
                    int m = (Integer) v.getTag();
                    int length = t_list.size();
                    for (int i = 0; i < length; i++){
                        if (i==m) {
                            t_list.get(m).findViewById(R.id.ly_va_cicy).setSelected(true);
                            t_list.get(m).findViewById(R.id.tv_va_cicy).setSelected(true);
                        }else{
                            t_list.get(i).findViewById(R.id.ly_va_cicy).setSelected(false);
                            t_list.get(i).findViewById(R.id.tv_va_cicy).setSelected(false);
                        }
                    }
//                    ly_va_cicy.setSelected(!ly_va_cicy.isSelected());
//                    if(ly_va_cicy.isSelected()){
//                        ly_va_cicy.setSelected(true);
//                        tv_va_cicy.setSelected(true);
//                    }else{
//                        ly_va_cicy.setSelected(false);
//                        tv_va_cicy.setSelected(false);
//                    }
                }
            });
        }
    }
    private void initEvent() {
        tv_va_all.setOnClickListener(this);
        iv_va_choose.setOnClickListener(this);
        manul_choice_fj.setOnClickListener(this);
        auto_choice_fj.setOnClickListener(this);
        tv_va_manul_start.setOnClickListener(this);
        tv_va_manul_end.setOnClickListener(this);

    }
   private ArrayList<String> vaManulList=new ArrayList<>();
    @Override
    public void onClick(View v){
        switch (v.getId()){
            //选择自动还是手动
            case R.id.iv_va_choose:
                iv_va_choose.setSelected(!iv_va_choose.isSelected());
                showDefault();
                if(iv_va_choose.isSelected()){
                   //自动
                    tv_va_choose.setText("自动");
                    ly_rb_auto.setVisibility(View.VISIBLE);
                    iv_va_choose.setSelected(true);
                    va_start.setChecked(true);
                    autoenevt();
                }else{
                  //手动
                    tv_va_choose.setText("手动");
                    ly_rb_manul.setVisibility(View.VISIBLE);
                    iv_va_choose.setSelected(false);
                    va_open.setChecked(true);
                    manulenevt();
                }
                break;
            //
            case R.id.tv_va_all:
                va_line.setVisibility(View.GONE);
                tv_va_all.setVisibility(View.GONE);
                break;
            //选择手动风机
            case R.id.manul_choice_fj:
                manulPop=new LayoutPopWindow(getActivity(),R.layout.pop_window_wp,va_manul_fj);
                ListView listView=manulPop.getView(R.id.pop_wp_lists);
                listView.setAdapter(new PopWindowWpAdapter(getActivity(), DataUtil.fenji,"号风机"));
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        va_manul_fj.setText(DataUtil.fenji[position]+"号风机");
                        manulPop.dismiss();
                        vaManulList.add(DataUtil.fenji[position]+"");
                        ly_choice_va_manul.setVisibility(View.VISIBLE);
                        va_manulgrid.setAdapter(new VarbGridViewAdapter(getActivity(),DataUtil.getRListData(vaManulList),"号风机"));
                    }
                });
                break;
            //选择手动风机
            case R.id.auto_choice_fj:
                autoPop=new LayoutPopWindow(getActivity(),R.layout.pop_window_wp,va_manul_fj);
                ListView listViews=autoPop.getView(R.id.pop_wp_lists);
                listViews.setAdapter(new PopWindowWpAdapter(getActivity(), DataUtil.fenji,"号风机"));
                listViews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        va_auto__fj.setText(DataUtil.fenji[position]+"号风机");
                        autoPop.dismiss();
                        vaManulList.add(DataUtil.fenji[position]+"");
                        ly_choice_va_auto.setVisibility(View.VISIBLE);
                        va_autogrid.setAdapter(new VarbGridViewAdapter(getActivity(),DataUtil.getRListData(vaManulList),"号风机"));
                    }
                });
                break;
            case R.id.tv_va_manul_end:
                MemoPopWin pickerPopWinss= new MemoPopWin.Builder(getActivity(), new MemoPopWin.OnDatePickedListener() {
                    @Override
                    public void onDatePickCompleted(String dateDesc){
                        tv_va_manul_end.setText(dateDesc);
                    }
                }).textConfirm("确定")
                        .textCancel("取消")
                        .btnTextSize(16)
                        .viewTextSize(20)
                        .btnTextSize(Color.parseColor("#333333"))
                        .colorCancel(Color.parseColor("#333333"))
                        .colorConfirm(Color.parseColor("#333333"))
                        .build();
                pickerPopWinss.showPopWin(getActivity());
                break;
            case R.id.tv_va_manul_start:
                MemoPopWin pickerPopWin = new MemoPopWin.Builder(getActivity(), new MemoPopWin.OnDatePickedListener() {
                    @Override
                    public void onDatePickCompleted(String dateDesc) {
                        tv_va_manul_start.setText(dateDesc);
                    }
                }).textConfirm("确定")
                        .textCancel("取消")
                        .btnTextSize(16)
                        .viewTextSize(20)
                        .btnTextSize(Color.parseColor("#333333"))
                        .colorCancel(Color.parseColor("#333333"))
                        .colorConfirm(Color.parseColor("#333333"))
                        .build();
                pickerPopWin.showPopWin(getActivity());
                break;
        }
    }
    private void showDefault(){
        ly_rb_manul.setVisibility(View.GONE);
        ly_rb_auto.setVisibility(View.GONE);
    }

    private void autoenevt(){
        rd_va_auto.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //启用
                if (checkedId == R.id.va_start) {

                }
                //停用
                else if (checkedId == R.id.va_stop) {

                }
            }
        });
    }
    private void manulenevt(){
        rd_va_manul.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //打开
                if (checkedId == R.id.va_open) {

                }
                //关闭
                else if (checkedId == R.id.va_close) {

                }
            }
        });
    }
}
